import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import {
  getSwiper,
  getRecommendCourses,
  getNoticeList,
  getOfficialtrainingList,
  getTeacherList,
  getSpecialSubjects,
  getCommonList,
  getIndexAd,
} from "@/api/index";
import Login from "@/components/Login";
import UserInfo from "@/components/Login/userInfo";
import store from "../../store";

var that;
export default {
  name: "index",
  components: {
    Pagination,
    Login,
    UserInfo,
  },
  data() {
    return {
      swiper1: {
        // 设置分页器
        pagination: {
          el: ".swiper-pagination1",
          // 设置点击可切换
          clickable: true,
        },
        // 设置自动轮播
        autoplay: {
          delay: 3000, // 5秒切换一次
        },
        // 设置轮播可循环
        loop: false,
        on: {
          click(e) {
            that.swiperList.forEach((item) => {
              if (item.id == e.target.dataset.id) {
                that.toSwiper(item);
              }
            });
          },
        },
      },
      swiper2: {
        // 设置分页器
        pagination: {
          el: ".swiper-pagination1",
          // 设置点击可切换
          clickable: true,
        },
        // 设置自动轮播
        autoplay: {
          delay: 3000, // 5秒切换一次
        },
        // 设置轮播可循环
        loop: true,
        on: {
          click(e) {
            that.swiperList.forEach((item) => {
              if (item.id == e.target.dataset.id) {
                that.toSwiper(item);
              }
            });
          },
        },
      },
      swiperOption: {
        slidesPerView: 5,
        spaceBetween: 30,
        // 设置分页器
        pagination: {
          el: ".swiper-pagination",
          // 设置点击可切换
          clickable: true,
        },
        // 设置自动轮播
        autoplay: {
          delay: 3000, // 5秒切换一次
        },
        // 设置轮播可循环
        loop: false,
      },
      swiperTeacherOption: {
        slidesPerView: 4,
        spaceBetween: 40,
        // 设置分页器
        pagination: {
          el: ".swiper-pagination",
          // 设置点击可切换
          clickable: true,
        },
        // 设置前进后退按钮
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
        // 设置轮播可循环
        loop: false,
      },
      swiperList: [],
      recommendCourseList: [],
      noticeList: [],
      officialtrainingList: [],
      teacherList: [],
      specialList: [],
      commonList: [],
      adsList: [],
      adsList2: [],
      adsList3: [],
      noticeType: 1,
      requiredNews:[],
      pcImgList: [],
      pcImgFlag: false
    };
  },
  computed: {
    swiperListOption() {
      var len = this.swiperList.length;
      if (len == 1) {
        return this.swiper1;
      } else {
        return this.swiper2;
      }
    },
  },

  methods: {
    onSwiper() {},
    onSlideChange() {},
    getAds() {
      var data = {
        adType: "PC",
        adPosition: "6",
        appId: "",
        limit: "1",
        time: "",
      };
      getIndexAd(data)
        .then((response) => {
          this.adsList = response.data;
        })
        .catch((err) => {
          this.$message.error(err.message || "error");
        });
    },
    getAds2() {
      var data = {
        adType: "PC",
        adPosition: "7",
        appId: "",
        limit: "2",
        time: "",
      };
      getIndexAd(data)
        .then((response) => {
          this.adsList2 = response.data;
        })
        .catch((err) => {
          this.$message.error(err.message || "error");
        });
    },
    getPiaoChuangAds() {
      var data = {
        adType: 'PC',
        adPosition: '10',
        appId: '',
        limit: '1',
        time: ''
      }
      getIndexAd(data).then((response) => {
        this.pcImgList = response.data
        console.log(this.pcImgList)
        if (this.pcImgList.length !== 0) {
          this.pcImgFlag = true
          var ad = new AdMove('Div2')
          ad.Run()
        }
      }).catch((err) => {
        this.$message.error(err.message || 'error')
      })
    },
    getAds3() {
      var data = {
        adType: "PC",
        adPosition: "8",
        appId: "",
        limit: "2",
        time: "",
      };
      getIndexAd(data)
        .then((response) => {
          this.adsList3 = response.data;
        })
        .catch((err) => {
          this.$message.error(err.message || "error");
        });
    },
    toDetail(id, NOTICETYPE) {
      this.$router.push({
        path: "/notice/detail",
        query: {
          NOTICEID: id,
          NOTICETYPE,
        },
      });
    },
    showTcourse(id) {
      this.$router.push({
        path: "/special-info",
        query: {
          trainingId: id,
        },
      });
    },
    toClass(id) {
      this.$router.push({
        path: "/course/singleCourseInfo",
        query: {
          courseId: id,
        },
      });
    },
    getSwiperList() {
      var data = {
        adType: "PC",
        appId: "",
        limit: "",
        time: "",
      };
      getSwiper(data)
        .then((response) => {
          this.swiperList = response.data;
        })
        .catch((err) => {
          this.$message.error(err.message || "error");
        });
    },
    getRecommendCoursesList() {
      var limit =10;
      getRecommendCourses(limit)
        .then((response) => {
          this.recommendCourseList = response.data;
        })
        .catch((err) => {
          this.$message.error(err.message || "error");
        });
    },
    getNotice() {
      var limit = 4;
      getNoticeList(limit)
        .then((response) => {
          this.noticeList = response.data;
        })
        .catch((err) => {
          this.$message.error(err.message || "error");
        });
    },
    getOfficialtraining() {
      var limit = 4;
      getOfficialtrainingList(limit)
        .then((response) => {
          this.officialtrainingList = response.data;
        })
        .catch((err) => {
          this.$message.error(err.message || "error");
        });
    },
    getTeacher() {
      var limit = 14;
      getTeacherList(limit)
        .then((response) => {
          this.teacherList = response.data;
        })
        .catch((err) => {
          this.$message.error(err.message || "error");
        });
    },
    getSpecial() {
      var limit = 14;
      getSpecialSubjects(limit)
        .then((response) => {
          this.specialList = response.data;
        })
        .catch((err) => {
          this.$message.error(err.message || "error");
        });
    },
    getCommon() {
      getCommonList(5)
        .then((response) => {
          this.commonList = response.data;
        })
        .catch((err) => {
          this.$message.error(err.message || "error");
        });
    },
    toCommonQuestion(id) {},
    toSwiper(swiper) {
      if (swiper.isLogin == 0) {
        //必须登录
        this.openUrl(swiper.adOpenWay, swiper.adUrl);
      } else {
        if (this.$store.state.user.token) {
          this.openUrl(swiper.adOpenWay, swiper.adUrl);
        } else {
          this.$alert("请先登录", "提示", {
            confirmButtonText: "确定",
          });
        }
      }
    },
    openUrl(adOpenWay, adUrl) {
      if (adOpenWay === "2") {
        //新页面吧
        window.open(adUrl, "_blank");
      } else if (adOpenWay === "3") {
        window.open(adUrl, "_self");
      }
    },
    toMore(){
      if(this.noticeType == 1){
        this.$router.push('/notice')
      }else{
        this.$router.push('/officialtrainingAdmin')
      }
    },
  },
  mounted() {
    that = this;
    this.getSwiperList();
    this.getAds();
    this.getAds2();
    this.getAds3();
    this.getRecommendCoursesList();
    this.getNotice();
    this.getOfficialtraining();
    this.getTeacher();
    this.getSpecial();
    this.getCommon();
    this.getPiaoChuangAds();
    this.$store.dispatch("app/getFooter").then(() => {
      document.title = this.$store.getters.footer.appTitle;
    });
  },
};
